

<h1>Role</h1>
<table class="items table">
	<thead>
		<tr>
			<th align="center">Menu Name</th>
			<?php foreach($level as $le): ?>
			<th align="center"><?php echo trim($le->name); ?></th>
			<?php endforeach; ?>
		</tr>
	</thead>
	<tbody>
	<?php foreach($data as $item): ?>
		<tr style="background:#999">
			<td align="canter"><strong><?php echo $item->menu_name; ?></strong></td>
			<?php foreach($level as $le): ?>
			<td align="left"></td>
			<?php endforeach; ?>
		</tr>
		<?php 
		$child1 = RoleMenu::model()->findAll('parent_id='.$item->id);
		foreach($child1 as $child):
		?>
		<tr>
			<td align="canter">-- <?php echo $child->menu_name; ?></td>
			<?php foreach($level as $le): ?>
			<td align="left">
			<input <?php echo RoleMenuAccess::model()->find('role_id='.$child->id.' and level='.$le->id)->access_status == 1 ? 'checked' : '' ?> type="checkbox" data-level = <?php echo $le->id; ?> data-id = <?php echo $child->id ?> name="acces_value[]" class="choosen" />
			</td>
			<?php endforeach; ?>
		</tr>
			<?php 
			$child2 = RoleMenu::model()->findAll('parent_id='.$child->id);
			foreach($child2 as $child2):
			?>
			<tr>
				<td align="canter">-- -- <?php echo $child2->menu_name; ?></td>
				<?php foreach($level as $le): ?>
				<td align="left"><input <?php echo RoleMenuAccess::model()->find('role_id='.$child2->id.' and level='.$le->id)->access_status == 1 ? 'checked' : '' ?> type="checkbox" data-level = <?php echo $le->id; ?> data-id = <?php echo $child2->id ?> name="acces_value[]" class="choosen" /></td>
				<?php endforeach; ?>
			</tr>
			<?php endforeach; ?>
		<?php endforeach; ?>
	<?php endforeach; ?>
	</tbody>
</table>
<?php  ?>

<script>
$(document).ready(function(){
	var cbs = $('.choosen');
	cbs.click(function() {
			if ($(this).is(':checked')){
				$.ajax({
					url: '<?php echo Yii::app()->createUrl('/roleMenuAccess/create'); ?>',
					dataType: 'json',
					type: 'post',
					data: 'data-level='+ $(this).attr('data-level') + '&data-id=' + $(this).attr('data-id') + '&access_value=1',
					success: function( data, textStatus, jQxhr ){
						console.log( data );
					},
					error: function( jqXhr, textStatus, errorThrown ){
						console.log( errorThrown );
					}
				});
			}else if($(this).trigger(':unchecked')){
				$.ajax({
					url: '<?php echo Yii::app()->createUrl('/roleMenuAccess/create'); ?>',
					dataType: 'json',
					type: 'post',
					data: 'data-level='+ $(this).attr('data-level')+'&data-id='+$(this).attr('data-id')+'&access_value=0',
					success: function( data, textStatus, jQxhr ){
						console.log( data );
					},
					error: function( jqXhr, textStatus, errorThrown ){
						console.log( errorThrown );
					}
				});
			}
	});
});
</script>
